package com.tensquare.article.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.article.dao.ChannelDao;
import com.tensquare.article.pojo.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class ChannelService {
    @Autowired
    private ChannelDao channelDao;
    @Autowired
    private IdWorker idWorker;

    public void save(Channel channel) {
        String id=idWorker.nextId()+"";
        channel.setId(id);
        channelDao.insert(channel);
    }

    public List<Channel> findAll() {
        return  channelDao.selectList(null);
    }

    public Channel findById(String channel) {
       return channelDao.selectById(channel);
    }

    public void updateById(Channel channel) {
        channelDao.updateById(channel);
    }

    public void deleteById(Channel channel) {
        channelDao.deleteById(channel);

    }

    public Page<Channel> findByPage(Map<String, Object> map, Integer page, Integer size) {
        //设置查询条件
        EntityWrapper<Channel> wrapper=new EntityWrapper<>();
        Set<String>set=map.keySet();
        for (String s : set) {
            wrapper.eq(map.get(s)!=null,s,map.get(s));
        }
        //设置分页参数
        Page<Channel> pageData=new Page<>(page,size);
        //查询返回结果集
        List<Channel> list = channelDao.selectPage(pageData, wrapper);
        //设置返回参数
        pageData.setRecords(list);
        return pageData;
    }
}
